#ifndef _FRONTENDCOMMUNICATOR_H_
#define _FRONTENDCOMMUNICATOR_H_

#include <online-certifier/util/typeDefines.hh>
#include <online-certifier/user-functions/userModularFunctionBase.hh>
#include <string>
#include <queue>
#include "communicatorUtil.hh"

using namespace std;

enum e_where{GRID, GRID5000, LOCAL};
enum e_grid_status { RUNNING, FAILURE, FINISH};
class frontendCommunicator {

    public:
        frontendCommunicator( userModularFunctionBase* t,
                string task_name,
                size_t grain_size,
                string kaapiTask_dir, 
                string grid_data_dir,
                string machine_file_addr,
                string  decoder_addr,
                int decoder_port, 
                string grid_addr,
                int grid_port,
                string job_key,
                e_where w,
                string grid_public);
       
        e_status terminate();
        e_status connectToDecoder();
    
        e_status writeToDecoder(size_t k, size_t errors);
      
        e_status readFromDecoder(integer* res, size_t* errors);
        e_status terminateDecoder();


        e_status dispatchGridTasks(size_t first, size_t last); //through ssh
        e_grid_status checkGridStatus();
    private:
        int decoderPort, gridPort;
        string decoderAddr;
        string gridAddr;
        int decoderSock;
        size_t grain;
        char recv_data[FRONTEND_BUFFER_SIZE];
        std::queue < std::string > resultQueue;//from decoder
        
        string kaapiTaskDir; // where kaapi task is located
        string gridDataDir;
        string taskName; //

        string grid_machine_file_addr; // machine names 
        
        string oarsh_job_key_file;
        string grid_karun_machine;
        string grid_public_machine;
        e_where where;
        string errfile; //tmp file used to store grid errors
        userModularFunctionBase* task;       

};

#endif
